package sort;

import java.util.Arrays;

/**
 * @Author Zhouchb
 * @Create 2021-10-2021/10/26 :16:44
 * @Description
 */
public class InsertSort {
    public static void main(String[] args) {
        Integer[] i = {13,3, 9, 1, 8, 5, 4, 7};
        sort(i);
        System.out.println(Arrays.toString(i));
    }
    public static void sort(Comparable[] arr) {
     for(int i=1;i< arr.length;i++){
         for (int j=i;j>0;j--){
             if (compare(arr[j-1],arr[j])){
                 swap(arr,j-1,j);
             }else {
                 break;
             }
         }
     }
    }

    public static boolean compare(Comparable a, Comparable b) {
        return a.compareTo(b) > 0;
    }

    public static void swap(Comparable[] arr, int i, int j) {
        Comparable c;
        c = arr[i];
        arr[i] = arr[j];
        arr[j] = c;
    }
}
